var colNames = null;
var colModel = null;
var groupHeaders = null;
var flag = false;
$(function() {
	$.ajax({
		type : "POST",
		url : baseURL + "contractTable/initialization?yearMonth="
				+ vm.q.yearMonth + "&dateMonthS=" + vm.q.dateMonthS
				+ "&dateMonthE=" + vm.q.dateMonthE,
		contentType : "application/json",
		success : function(r) {
			if (r.code === 0) {
				colNames = r.colNames;
				colModel = r.colModel;
				groupHeaders = r.groupHeaders;
				$('#dateMonthS').val(r.startMonth);
				$('#dateMonthE').val(r.endMonth);
				$('#dateYearS').val(r.startYear);
				$('#dateYearE').val(r.endYear);
				firstLoad();

			} else {
				alert(r.msg);
			}
		}
	});
	$('#dateMonthS').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				$('#dateMonthS').val(year + '-' + month);
				vm.q.dateMonthS = year + '-' + month;
				$(this).datepicker('close');
			});
	$('#dateMonthE').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				$('#dateMonthE').val(year + '-' + month);
				vm.q.dateMonthE = year + '-' + month;
				$(this).datepicker('close');
			});

	$('#dateYearS').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				$('#dateYearS').val(year);
				vm.q.dateYearS = year;
				$(this).datepicker('close');
			});
	$('#dateYearE').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				$('#dateYearE').val(year);
				vm.q.dateYearE = year;
				$(this).datepicker('close');
			});
});
function firstLoad() {
	$("#jqGridTable").empty().html(
			'<table id="jqGrid"></table><div id="jqGridPager"></div>');
	if (vm.q.yearMonth == 2) {
		vm.q.dateMonthS = $('#dateYearS').val();
		vm.q.dateMonthE = $('#dateYearE').val();
	} else if (vm.q.yearMonth == 1) {
		vm.q.dateMonthS = $('#dateMonthS').val();
		vm.q.dateMonthE = $('#dateMonthE').val();
	}
	vm.q.item = $(".am-selected-status.am-fl").text();
	$("#jqGrid").jqGrid(
			{
				url : baseURL + 'contractTable/contractDetail',
				datatype : "json",
				postData : {
					'yearMonth' : vm.q.yearMonth,
					'dateMonthS' : vm.q.dateMonthS,
					'dateMonthE' : vm.q.dateMonthE,
					'buildIds' : vm.q.buildIds,
					'assetIds' : vm.q.assetIds,
					'contractNo' : vm.q.contractNo,
					'item' : vm.q.item
				},
				colNames : colNames,
				colModel : colModel,
				viewrecords : true,
				height : 385,
				rowNum : 10,
				rowList : [ 10, 30, 50 ],
				rownumbers : true,
				rownumWidth : 25,
				autowidth : true,
				shrinkToFit : false,
				footerrow : true,
				sortable : false,
				pager : "#jqGridPager",
				jsonReader : {
					root : "page.list",
					page : "page.currPage",
					total : "page.totalPage",
					records : "page.totalCount"
				},
				prmNames : {
					page : "page",
					rows : "limit",
					order : "order"
				},
				gridComplete : function() {
					// 冻结列
					$("#jqGrid").jqGrid('setFrozenColumns');
					// jqGrid底部合计
					var rowNum = parseInt($(this).getGridParam("records"), 10);
					if (rowNum > 0) {
						var jz = {};
						$(".ui-jqgrid-sdiv").show();
						for (var i = 3; i < colModel.length; i++) {
							jz[colModel[i].name] = $(this).getCol(
									colModel[i].name, false, "sum");
						}
						$(this).footerData("set", jz); // 将合计值显示出来
					} else {
						$(".ui-jqgrid-sdiv").hide();
					}
					airbus();
					frozenDislocation();
				}
			});
	$("#jqGrid").jqGrid("setGroupHeaders", {
		useColSpanStyle : true,// 没有表头的列是否与表头所在行的空单元格合并
		groupHeaders : groupHeaders
	});
};
function airbus() {
	$(".ui-jqgrid-sdiv").find("td[aria-describedby='jqGrid_buildIds']").empty()
			.text("合计");
	$(".frozen-sdiv.ui-jqgrid-sdiv").find(
			"td[aria-describedby='jqGrid_buildIds']").empty().text("合计");
};
function frozenDislocation() {
	// 冻结错位解决
	var bdivTop = -1;
	var bdivHeight = -1;
	var sdivTop = -1;
	// opera 不需要hack
	if (navigator.userAgent.indexOf("Opera") >= 0) {
		divTop = 0;
		bdivTop = 0;
	}
	$('.frozen-bdiv.ui-jqgrid-bdiv').css({
		top : $('.frozen-bdiv.ui-jqgrid-bdiv').position().top + bdivTop
	});
	$('.frozen-sdiv.ui-jqgrid-sdiv').css({
		top : $('.frozen-sdiv.ui-jqgrid-sdiv').position().top + sdivTop
	});
	setTimeout(function() {
		$('.frozen-bdiv.ui-jqgrid-bdiv').height(
				$('.frozen-bdiv.ui-jqgrid-bdiv').height() + bdivHeight)
	}, 100);
};
var vm = new Vue({
	el : '#rrapp',
	data : {
		showList : true,
		q : {
			yearMonth : '1',
			assetIds : '',
			buildIds : '',
			dateMonthE : '',
			dateMonthS : '',
			dateYearE : '',
			dateYearS : '',
			contractNo : ''
		}
	},
	methods : {
		exportExcel : function() {
			if (vm.q.yearMonth == 2) {
				vm.q.dateMonthS = $('#dateYearS').val();
				vm.q.dateMonthE = $('#dateYearE').val();
			} else if (vm.q.yearMonth == 1) {
				vm.q.dateMonthS = $('#dateMonthS').val();
				vm.q.dateMonthE = $('#dateMonthE').val();
			}
			vm.q.item = $(".am-selected-status.am-fl").text();
			location.href = baseURL
					+ "contractTable/contractDetailExport?yearMonth="
					+ vm.q.yearMonth + "&dateMonthE=" + vm.q.dateMonthE
					+ "&dateMonthS=" + vm.q.dateMonthS + "&assetIds="
					+ vm.q.assetIds + "&buildIds=" + vm.q.buildIds
					+ "&contractNo=" + vm.q.contractNo + "&item=" + vm.q.item
					+ "&token=" + token;
		},
		query : function() {
			if (vm.q.yearMonth == 2) {
				vm.q.dateMonthS = $('#dateYearS').val();
				vm.q.dateMonthE = $('#dateYearE').val();
			} else if (vm.q.yearMonth == 1) {
				vm.q.dateMonthS = $('#dateMonthS').val();
				vm.q.dateMonthE = $('#dateMonthE').val();
			}
			vm.q.item = $(".am-selected-status.am-fl").text();
			$.ajax({
				type : "POST",
				url : baseURL + "contractTable/initialization?yearMonth="
						+ vm.q.yearMonth + "&dateMonthS=" + vm.q.dateMonthS
						+ "&dateMonthE=" + vm.q.dateMonthE + "&item="
						+ vm.q.item,
				contentType : "application/json",
				success : function(r) {
					if (r.code === 0) {
						colNames = r.colNames;
						colModel = r.colModel;
						groupHeaders = r.groupHeaders;
						firstLoad();
					} else {
						alert(r.msg);
					}
				}
			});
		},
		reload : function(event) {

		}
	}
});
